package com.WOWTradeNews.phone;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.WOWTradeNews.DataBase.Country;
import com.WOWTradeNews.DataBase.DBManager;
import com.WOWTradeNews.DataBase.News;
import com.WOWTradeNews.services.NewsServer;
import com.WOWTradeNewsTest.R;
import com.WOWTradeNews.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

public class SearchHistry extends Activity implements OnClickListener, OnTouchListener {
	private EditText begTime;
	private EditText endTim;
	private EditText titKey;
	private EditText cntKey;
	private EditText country;
	private EditText subject;
	
	private Button btn_srh;
	private Button btn_back;
	private Button btn_clear;
	private Button backButton;
	
	private int subjectAns = 0;
	
	private static final String[] subjects = {
		"全部", "市調菁華", "專題研析", "商情放送", "活動快訊", "經貿書訊"
	};
	
	private String[] countries;
	private String[] countriesIid;
	private int countryId;
	private int ctyCnt;
	
	@Override
	public void onCreate(Bundle savedInstanceState)  {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_histry);
		
		initMember();
		
	}
	
	private void initMember() {
		begTime = (EditText) findViewById(R.id.begTime);
		endTim = (EditText) findViewById(R.id.endTime);
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);  
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        begTime.setText(year + "/" + (month - 3) + "/" + day);
        endTim.setText(year + "/" + month + "/" + day);
        
		titKey = (EditText) findViewById(R.id.title_key);
		cntKey = (EditText) findViewById(R.id.cont_key);
		country = (EditText) findViewById(R.id.srh_country);
		subject = (EditText) findViewById(R.id.srh_subject_name);
		
		btn_srh = (Button) findViewById(R.id.srh_btn_srh);
		btn_back = (Button) findViewById(R.id.srh_btn_back);
		btn_clear = (Button) findViewById(R.id.srh_btn_clear);
		
		backButton = (Button) findViewById(R.id.back);
        backButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		begTime.setOnTouchListener(this);
		begTime.setKeyListener(null);
		endTim.setOnTouchListener(this);
		//endTim.setKeyListener(null); in xml 
		subject.setOnTouchListener(this);
		subject.setKeyListener(null);
		
		country.setOnTouchListener(this);
		country.setKeyListener(null);
		
		btn_srh.setOnClickListener(this);
		btn_back.setOnClickListener(this);
		btn_clear.setOnClickListener(this);
		
		
		List<Country> list = null;
		try {
			list = NewsServer.getCountriesList();
		} catch (Exception e) {
			Dialog.showDialog(this, "獲取國家信息失敗");
			e.printStackTrace();
		}
		
		countryId = 0;
		if(list != null) {
			ctyCnt = 1;
			countries = new String[list.size()+1];
			countriesIid = new String[list.size()+1];
			countries[0] = "全部";
			countriesIid[0] = "";
			for(Country country : list) {
				 countries[ctyCnt] = country.getName();
				 countriesIid[ctyCnt] = country.getName();
				 ctyCnt++;
			}
			if(countries != null) {
				country.setText(countries[countryId]);
			}
		}
		
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch (id) {
		case R.id.srh_btn_back:
			finish();
			break;
		case R.id.srh_btn_clear:
			clearAll();
			break;
		case R.id.srh_btn_srh:
			search();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onTouch(View view, MotionEvent event) {
		int id = view.getId();
		
		if(event.getAction() == MotionEvent.ACTION_UP) {
			switch (id) {
			case R.id.begTime:
				showDateDialg(id);
				break;
			case R.id.endTime:
				showDateDialg(id);
				break;
			case R.id.srh_subject_name:
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("單元");
				builder.setSingleChoiceItems(subjects, subjectAns, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						subjectAns = which;
						Log.d("zhaosdfa", "---which-->>>" + which);
					}
				});
				builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						subject.setText(subjects[subjectAns]);
						Log.d("zhaosdfa", "---onClick-->>>" + subjectAns);
					}
				});
				builder.show();
				break;
			case R.id.srh_country:
				AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
				builder1.setTitle("活動國家");
				builder1.setSingleChoiceItems(countries, countryId, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						countryId = which;
					}
				});
				builder1.setPositiveButton("確定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						if(countries != null) {
							country.setText(countries[countryId]);
						}
					}
				});
				builder1.show();
				break;
			default:
				break;
			}
		}
		
		return false;
	}
	
	private void clearAll() {
//		begTime.setText("");
//		endTim.setText("");
		titKey.setText("");
		cntKey.setText("");
		subject.setText("全部");
		subjectAns = 0;
		country.setText("全部");
	}
	
	private void showDateDialg(final int id) {
		Calendar calendar = Calendar.getInstance();
		DatePickerDialog dialog = new DatePickerDialog(this,  
                new DatePickerDialog.OnDateSetListener() {
					
					@Override
					public void onDateSet(DatePicker datePicker, int year, int mouth, int dayOfMonth) {
						EditText t = (EditText) findViewById(id);
						t.setText(String.format("%d/%d/%d", year, mouth+1, dayOfMonth));
					}
				},  
                calendar.get(Calendar.YEAR),  
                calendar.get(Calendar.MONTH),  
                calendar.get(Calendar.DAY_OF_MONTH));
		dialog.show();
	}
	
	private void search() {
		if(!Utils.isConnect(this)) {
			Dialog.showDialog(this, "無網路連結!");
			return ;
		}
		//"theme", "keywords", "s_keywords", "country", "city" ,"date1", "date2", "search"
		
		Intent i = new Intent();
		i.setClass(SearchHistry.this, TradeNewsList.class);
		Bundle bundle = new Bundle();
		bundle.putString("type", "11");
		bundle.putString("subject", "查詢結果");
		
		bundle.putString("keywords", cntKey.getText().toString());
		bundle.putString("s_keywords", titKey.getText().toString());
		String ct = countriesIid[countryId];
		if(ct.equals("全部")) {
			ct = "";
		}
		bundle.putString("country", ct);
		bundle.putString("city", "");
		bundle.putString("date1", begTime.getText().toString());
		bundle.putString("date2", endTim.getText().toString());
		bundle.putString("search", "yes");
		if(subjectAns == 0) 
			bundle.putString("theme", "");
		else
			bundle.putString("theme", String.valueOf(subjectAns + 1));
		
		i.putExtras(bundle);
		startActivity(i);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		Log.d("zhaosdfa", "---resume-->>>" + subjectAns);
		if(subject != null) {
			subject.setText(subjects[subjectAns]);
			Log.d("zhaosdfa", "---resume--if-->>>" + subjects[subjectAns]);
		}
	}
}
